package datebase

import (
	"fmt"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func init() {
	//1.数据库的 用户名，密码，数据安装的ip
	dsn := "root:123456@tcp(172.14.1.161:3306)/classmates"
	var err error
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("连接失败", err)
		return
	}
	fmt.Println(DB)
	fmt.Println("连接成功：")
}

// 全局数据库
var DB *gorm.DB

type Records struct {
	ID         int
	Name       string
	Sn         string
	Tel        string
	Comments   string
	Classid    int
	Createtime time.Time
	Classroom  string
}

func Kaoqin() {
	var zskaoqin Records
	zskaoqin.Name = "张三"
	zskaoqin.Sn = "123456"
	zskaoqin.Tel = "147285369"
	zskaoqin.Classid = 3

}

type KaoqinRecord struct {
	Name string
	Num  int
}

// 统计次数
func QueryDB() {
	var name string
	fmt.Println("请输入你要查询学生的名字")
	fmt.Scanln(&name)
	var result []Records
	DB.Raw(`select*from classmates.records where name=?`, name).Scan(&result)

	for _, item := range result {
		fmt.Println(item.Name, "出勤", item.Createtime, " 分数", item.Comments)
	}
}
